/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import static Database.Connect.ConnectMySQL;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hai
 */
public class post extends HttpServlet {

    private String currentTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String string = dateFormat.format(date);
        return string;
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);

        String chuyenMuc = (String) request.getParameter("chuyenMuc");
        String sSQL = "INSERT INTO `travel`.`" + chuyenMuc + "` (`Title`, `Content`, `ThoiGianPost`, `IDNguoiPost`) VALUES ( ?, ?, ?, ?);";

        
        String title = (String) request.getParameter("title");
        String content = (String) request.getParameter("content");
        String id = (String) session.getAttribute("id");

        try {

            PreparedStatement pst = ConnectMySQL().prepareStatement(sSQL);

            pst.setString(1, title);
            pst.setString(2, content);
            pst.setString(3, currentTime());
            pst.setString(4, id);

            if (pst.executeUpdate() >= 1) {
                response.sendRedirect(Const.SiteConstant.pageList + "?where="+chuyenMuc);
            }
        } catch (Exception e) {
        } finally {
            out.close();
        }

    }

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request
     * @param response
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try{
        processRequest(request, response);
        }catch(ServletException e)
        {
            
        } catch (IOException e) {
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
